package com.ldw.mulptiThread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 让三个线程几乎同时启动
 */
public class SimultaneousThreads {
    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(1);

        ExecutorService executorService = Executors.newFixedThreadPool(3);
        executorService.submit(() -> runTask(countDownLatch));
        executorService.submit(() -> runTask(countDownLatch));
        executorService.submit(() -> runTask(countDownLatch));
        countDownLatch.countDown();

        // 关闭线程池
        executorService.shutdown();
    }


    private static void runTask(CountDownLatch latch) {
        try {
            latch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("task " + Thread.currentThread() + "  running");
    }

}
